package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import multiformat.Base;
import multiformat.Calculator;
import multiformat.Format;
import multiformat.FormatException;
import multiformat.NumberBaseException;

public class CalculatorModel{
	
	Calculator calc;
	private ArrayList<ActionListener> actionListenerList = new ArrayList<ActionListener>();
	private String outputString;	
	
	public CalculatorModel(){
		calc = new Calculator();
	}
	
	public String firstOperand(){
		return calc.firstOperand();
	}
	
	public String secondOperand(){
		return calc.secondOperand();
	}
	
	public void addOpperand(String newOperand) throws NumberBaseException, FormatException{
		calc.addOperand(newOperand);
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void add(){
		calc.add();
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void substract(){
		calc.subtract();
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void multiply(){
		calc.multiply();
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void divide(){
		calc.divide();
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void setBase(Base newBase){
		calc.setBase(newBase);
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public void setFormat(Format newFormat){
		calc.setFormat(newFormat);
		processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
	}
	
	public String getOutputString(){
		return this.outputString;
	}
	public void setOutputString(String operation, String option, String firstOp, String secondOp){
		if(operation == "operator"){
			this.outputString = "Het antwoord van "+firstOp + " " + option + " " + secondOp +" = " + secondOperand()+"\n";
		}
		if(operation == "format"){
			this.outputString = "Het format is veranderd naar " + option + "\n";
		}
		if(operation == "base"){
			this.outputString = "Het talstelsel is verandert naar " + option + "\n";
		}
		
	}
	
	public void addActionListener( ActionListener l){
		actionListenerList.add( l );
	}

	public void removeActionListener( ActionListener l){
		if ( actionListenerList.contains( l ) )
			actionListenerList.remove( l );
	}
	
	private void processEvent(ActionEvent e){
		for( ActionListener l : actionListenerList)
			l.actionPerformed( e );
	}
}